package com.aopeng.filter;

import com.aopeng.entity.ClientInfo;
import com.aopeng.mapper.ClientInfoMapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author <a href="mailto:juhui.chen@vtradex.com">陈局辉</a>
 * @since 2020-09-25 14:56
 */
@Component
@WebFilter
@Order(1)
public class ClientInfoFilter implements Filter {

    @Autowired
    private ClientInfoMapper clientInfoMapper;
    private static final Set<String> ALLOWED_PATHS = Collections.unmodifiableSet(new HashSet<>(
        Arrays.asList("/js", "/img", "/css", "/font","/images","/scss","/vendor","/static")));

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
        throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        /*if (!request.getRequestURL().toString().startsWith("http://aopen100.com")&&!request.getRequestURL().toString().startsWith("http://www.aopen100.com")){
            response.sendRedirect("http://www.baidu.com/");
            return;
        }*/
        filterChain.doFilter(request, response);
    }

    /** 获取客户端ip信息 */
    public void saveClientInfo(HttpServletRequest request){
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.trim() == "" || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.trim() == "" || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.trim() == "" || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        // 多个路由时，取第一个非unknown的ip
        final String[] arr = ip.split(",");
        for (final String str : arr) {
            if (!"unknown".equalsIgnoreCase(str)) {
                ip = str;
                break;
            }
        }
        ClientInfo ci = new ClientInfo();
        System.out.println(ip);
        ci.setClientIp(ip);
        ci.setRemoteHost(request.getRemoteHost());
        ci.setRemotePort(request.getRemotePort());
        ci.setLocalAddr(request.getLocalAddr());
        ci.setLocalName(request.getLocalName());
        ci.setRequestUrl(request.getRequestURL().toString());
        System.out.println(ci.toString());
        clientInfoMapper.insert(ci);
    }

    @Override
    public void destroy() {

    }
}
